package com.ruoyi.subject.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.subject.domain.QbSubject;
import com.ruoyi.subject.service.IQbSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目Controller
 *
 * @author lzhj
 * @date 2021-01-20
 */
@Api(tags = "题目")
@RestController
@RequestMapping("/qb/subject")
public class QbSubjectController extends BaseController {
    @Autowired
    private IQbSubjectService qbSubjectService;

    /**
     * 查询题目列表
     */
    @ApiOperation(value = "查询题目列表,分页。qb:subject:list'")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", dataType = "int", paramType = "path"),
    })
    @PreAuthorize("@ss.hasPermi('qb:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(QbSubject qbSubject) {
        startPage();
        List<QbSubject> list = qbSubjectService.selectQbSubjectList(qbSubject);
        return getDataTable(list);
    }

    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('qb:subject:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QbSubject qbSubject) {
        List<QbSubject> list = qbSubjectService.selectQbSubjectList(qbSubject);
        ExcelUtil<QbSubject> util = new ExcelUtil<QbSubject>(QbSubject.class);
        return util.exportExcel(list, "subject");
    }

    /**
     * 获取题目详细信息
     */
    @ApiOperation(value = "获取题目详细信息。qb:subject:query")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "path")
    @PreAuthorize("@ss.hasPermi('qb:subject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(qbSubjectService.selectQbSubjectById(id));
    }

    /**
     * 新增题目
     */
    @ApiOperation(value = "新增题目信息，qb:subject:add")
    @PreAuthorize("@ss.hasPermi('qb:subject:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbSubject qbSubject) {
        int i = qbSubjectService.insertQbSubject(qbSubject);
//        if (i>0){
//            qbSubjectService.updateSubjectTypeCache(qbSubject.getCategory(), qbSubject.getSubClass(),qbSubject.getQueType());
//        }
        return toAjax(i);
    }

    /**
     * 修改题目
     */
    @ApiOperation(value = "修改题目信息，qb:subject:edit")
    @PreAuthorize("@ss.hasPermi('qb:subject:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbSubject qbSubject) {
        return toAjax(qbSubjectService.updateQbSubject(qbSubject));
    }

    /**
     * 删除题目
     */
    @ApiOperation(value = "删除题目信息，qb:subject:remove")
    @PreAuthorize("@ss.hasPermi('qb:subject:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        qbSubjectService.deleteQbSubjectByIds(ids);
        return AjaxResult.success("删除成功");
    }

    /**
     * 初始化题目，按题库类型，类别，题型 分类
     */
    @ApiOperation(value = "初始化题目，按题库类型，类别，题型 分类，qb:subject:InitSubject")
    @PreAuthorize("@ss.hasPermi('qb:subject:InitSubject')")
    @GetMapping("/InitSubjectTypeData")
    public AjaxResult InitSubjectTypeData() {
        logger.info("-----------初始化题目,按题库类型，类别，题型 分类存到缓存，初始化抽题规则到缓存");
        qbSubjectService.initSubjectTypeCacheData();
        qbSubjectService.initSubjectRule();
        return AjaxResult.success("初始化成功");
    }

}
